home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / Intuition / other_examples / CustGad / images.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  5.5 KB  |  234 lines

  1. /* images.c -- image utiltity routines :ts=4 */
  2.  
  3. /*
  4. Copyright (c) 1989 Commodore-Amiga, Inc.
  5.  
  6. Executables based on this information may be used in software
  7. for Commodore Amiga computers. All other rights reserved.
  8. This information is provided "as is"; no warranties are made.
  9. All use is at your own risk, and no liability or responsibility
  10. is assumed.
  11. */
  12.  
  13. #include "sysall.h"
  14.  
  15. #define D(x)    ;
  16. #define DI(x)    ;
  17.  
  18. /*
  19.  * will set up Image.Depth.  Feel free
  20.  * to change the PlanePick and PlaneOnOff fields
  21.  */
  22. struct Image    *
  23. CreateImage( width, height, depth )
  24. {
  25.     struct Image    *im;
  26.     int    rassize;
  27.     UWORD            *sizeword;
  28.  
  29.     im = (struct Image *) AllocMem( sizeof (struct Image) + sizeof( UWORD ),
  30.             MEMF_PUBLIC | MEMF_CLEAR );
  31.  
  32.     D( printf("CE: w %d h %d, im %lx, next %lx\n", width, height, im,
  33.         im->NextImage ) );
  34.  
  35.     if ( im )
  36.     {
  37.         sizeword = (UWORD *) (im + 1);
  38.  
  39.         rassize = RASSIZE( (long) width, (long) height );
  40.         D( printf( "CreateImage: rassize: %d\n", rassize ) );
  41.         *sizeword = rassize;
  42.  
  43.         im->ImageData = (USHORT *) AllocMem( (long) rassize * depth,
  44.             (long) MEMF_CHIP | MEMF_CLEAR );
  45.  
  46.         if ( im->ImageData )
  47.         {
  48.             im->Width = width;
  49.             im->Height = height;
  50.             im->Depth = depth;
  51.             im->PlanePick = ( 1 << depth ) - 1;
  52.         }
  53.         else
  54.         {
  55.             D( printf("CI free abort\n") );
  56.             FreeMem( im, (long) (sizeof *im) );
  57.             im = NULL;
  58.         }
  59.     }
  60.     return ( im );
  61. }
  62.  
  63. /*
  64.  * dont' rely on depth for raster size, since he's allowed to
  65.  * change that to pull mask tricks
  66.  */
  67. DeleteImage( im )
  68. struct Image    *im;
  69. {
  70.     UWORD *sizeword;
  71.  
  72.     sizeword = (UWORD *) (im + 1);
  73.  
  74.     D( printf("DeleteImage\n") );
  75.     FreeMem( im->ImageData,  (long) *sizeword );
  76.     FreeMem( im, (long) sizeof ( *im ) );
  77. }
  78.  
  79. /* area info capacity (MODIFY if you need more space)    */
  80. #define MAXAVECTORS    (20)
  81. #define AIBUFFSIZE    (5 * MAXAVECTORS)
  82.  
  83. #define IRP_SIZE ( (long) sizeof ( struct RastPort ) + \
  84.     sizeof ( struct BitMap ) + \
  85.     sizeof ( struct TmpRas ) + \
  86.     sizeof ( struct AreaInfo )  + AIBUFFSIZE )
  87.  
  88. /*
  89.  * create a rastport for drawing into an Intuition Image structure
  90.  * creates it with an initialized TmpRas and AreaInfo.
  91.  * modify this if you need more space in the AreaInfo.
  92.  */
  93. struct RastPort    *
  94. CreateImageRPort( im )
  95. struct Image    *im;
  96. {
  97.    /** These are all allocated contiguously    **/
  98.    struct RastPort    *rp;
  99.    struct BitMap    *bmap;
  100.    struct TmpRas    *tmpras;
  101.    struct AreaInfo    *ainfo;
  102.    char            *abuffer;
  103.  
  104.    int    i;
  105.    int    rassize;
  106.    UBYTE    *planeptr;
  107.    UBYTE    *tmpraster;
  108.  
  109.  
  110.    /* allocate rport and bmap contiguously    */
  111.    rp = (struct RastPort *) AllocMem( (long) IRP_SIZE, (long) MEMF_CLEAR );
  112.  
  113.    rassize = RASSIZE( (long) im->Width, (long) im->Height );
  114.  
  115.    D( printf( "CIRP: irpsize: %ld, rassize: %d\n", IRP_SIZE, rassize ) );
  116.  
  117.    if ( rp )
  118.    {
  119.     D( printf("cirp: rastport at: %lx\n", rp ) );
  120.     InitRastPort( rp );
  121.  
  122.     bmap = (struct BitMap *) &rp[1];
  123.     tmpras = (struct TmpRas *) &bmap[1];
  124.     ainfo = (struct AreaInfo *) &tmpras[1];
  125.     abuffer = (char *) &ainfo[1];
  126.  
  127.     /*** init the BitMap ***/
  128.  
  129.     D( printf( "cirp: init the bmap\n") );
  130.  
  131.     InitBitMap(  bmap, (long) im->Depth, 
  132.         (long) im->Width, (long) im->Height );
  133.  
  134.     /* point rastport at bitmap    */
  135.     rp->BitMap = bmap;
  136.  
  137.     /* point bitmap at image data    */
  138.     planeptr = (UBYTE *) im->ImageData;
  139.     for ( i = 0; i < im->Depth; ++i )
  140.     {
  141.         bmap->Planes[ i ] = (PLANEPTR) planeptr;
  142.         D( printf( "cirp: init plane: %lx\n", planeptr) );
  143.         planeptr += rassize;
  144.     }
  145.  
  146.     /*** init the AreaInfo ***/
  147.     D( printf("init the AInfo\n") );
  148.     InitArea( ainfo, abuffer, (long) MAXAVECTORS );
  149.     rp->AreaInfo = ainfo;
  150.  
  151.     /*** init the TmpRas ***/
  152.     D( printf("init the TmpRas\n") );
  153.     tmpraster = (UBYTE *) AllocMem( (long) rassize, (LONG) MEMF_CHIP );
  154.     if ( tmpraster )
  155.     {
  156.         D( printf("TR: %d bytes at %lx\n", rassize, tmpraster ) );
  157.         InitTmpRas( tmpras, tmpraster, (long) rassize );
  158.         rp->TmpRas = tmpras;
  159.         rp->RP_User = (APTR *) tmpraster;    /* for later freeing    */
  160.     }
  161.     else
  162.     {
  163.         D( printf("no tmpraster\n") );
  164.         FreeMem( rp, IRP_SIZE );
  165.         rp = NULL;
  166.     }
  167.    }
  168.    D( else printf("no rastport bundle\n") );
  169.  
  170.    D( printf("cirp: returning rastport %lx\n", rp ) );
  171.    return ( rp );
  172. }
  173.  
  174. /*
  175.  * only use on rastports created by CreateImageRPort
  176.  */
  177. DeleteImageRPort( rp )
  178. struct RastPort    *rp;
  179. {
  180.     WaitBlit();        /* tmpras no longer in use    */
  181.  
  182.     D( printf("DIRP: %lx\n", rp ) );
  183.  
  184.     D( printf("delete tmpraster at %lx\n", rp->RP_User ) );
  185.     D( printf("trsize: %d\n", rp->BitMap->Rows * rp->BitMap->BytesPerRow ) );
  186.  
  187.     /* tmpraster is same size as rastport bitmap    */
  188.     FreeMem( rp->RP_User, (long) rp->BitMap->Rows * rp->BitMap->BytesPerRow );
  189.  
  190.     D( printf("delete rastport at %lx\n", rp ) );
  191.  
  192.     FreeMem( rp, (long) IRP_SIZE );
  193. }
  194.  
  195. /*
  196.  * return pointer to one of the image planes
  197.  */
  198. UWORD    *
  199. ImagePlane( im, plane )
  200. struct Image    *im;
  201. {
  202.     int    planesize = RASSIZE( im->Width, im->Height );
  203.  
  204.     DI( printf("IP: planesize: %d\n", planesize ) );
  205.  
  206.     return ( im->ImageData + plane * (planesize/2) );
  207. }
  208.  
  209. clearWords( buff, numwords )
  210. WORD    *buff;
  211. {
  212.     while ( numwords-- ) *buff++ = 0;
  213. }
  214.  
  215. /* fancy this up later    */
  216. drawBox( rp, width, height, fieldcolor, bordercolor, vthick, hthick )
  217. struct RastPort    *rp;
  218. {
  219.     DI( printf("w %d h %d vthick %d hthick %d\n",
  220.         width, height, vthick, hthick ) );
  221.     SetAPen( rp, (long) bordercolor );
  222.     RectFill( rp, 0L, 0L, (long) width-1, (long) height-1 );
  223.  
  224.     SetAPen( rp, (long) fieldcolor );
  225.     RectFill( rp, (long) vthick, (long) hthick, (long) width-1 - vthick,
  226.         (long) height - 1 - hthick );
  227.  
  228.     DI( printf("outer rect: %d %d %d %d\n", 0, 0, width-1,
  229.         height - 1) );
  230.  
  231.     DI( printf("inner rect %d %d %d %d\n",
  232.         vthick, hthick, width-1 - vthick, height - 1 - hthick ) );
  233. }
  234.